package org.wattdepot.cli;

import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Executes the total carbon/energy generated command.
 * 
 * @author Scott Wong and Kelli Sawai
 * 
 */
public class ListTotalDay extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Executes and returns the results of the total carbon/energy generated command.
   * 
   * @param args The arguments required to execute this method.
   * @return The results of the command.
   */
  public String doCommand(String args) {
    try {
      if (args
          .matches("total [carbon | energy]+ generated \\S* day \\S* sampling-interval \\d*")) {
        String[] powerInput = args.split(" ");
        String type = powerInput[1];
        String sourceName = powerInput[3];
        String day = powerInput[5];
        int interval = Integer.parseInt(powerInput[7]);
        XMLGregorianCalendar startDay = toTimestamp(day);
        XMLGregorianCalendar endDay = Tstamp.incrementDays(startDay, 1);
        // Check if the timestamp is in the future.
        XMLGregorianCalendar now =
            DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(new GregorianCalendar());
        if (startDay.compare(now) >= 0) {
          return futureTimestampMessage;
        }
        return listTotalGenerated(type, sourceName, startDay, endDay, interval);
      }
      else {
        return "The command you have entered is invalid.";
      }
    }
    catch (WattDepotClientException e) {
      return "An error occured while executing the requested command." + e;
    }
    catch (Exception e) {
      return "An error occured while executing the requested command.";
    }
  }

  /**
   * Returns the total amount of carbon (or energy) generated by this source for the given day. The
   * sampling interval indicates how to generate the curve of carbon (or energy) data points.
   * 
   * @param type Whether it's carbon or energy generated
   * @param source The source
   * @param startDay The start of the day
   * @param endDay The end of the day
   * @param interval Interval in minutes
   * @return carbon or energy generated
   * @throws WattDepotClientException if an error occurs
   */
  private String listTotalGenerated(String type, String source,
      XMLGregorianCalendar startDay, XMLGregorianCalendar endDay, int interval)
      throws WattDepotClientException {
    try {
      if (type.compareToIgnoreCase("Carbon") == 0) {
        return Double.toString(client
            .getCarbonEmitted(source, startDay, endDay, interval));
      }
      else {
        return Double.toString(client.getEnergyGenerated(source, startDay, endDay,
            interval));
      }
    }
    catch (ResourceNotFoundException e) {
      return resourceMessage;
    }
  }
}
